package baseFx.tests.database.monitor;

import baseFx.common.context.BeanDef;
import baseFx.common.context.TypeResolver;
import baseFx.common.concurrent.Task;
import baseFx.database.AbstractSql;
import baseFx.database.execute.DbExecute;
import baseFx.database.execute.impl.MysqlExecute;
import baseFx.database.monitor.DbExecuteMonitor;
import baseFx.tests.database.TestSql;

import java.util.List;

public class DbExecuteMonitorTests {
    public static void main(String[] args) {
        AbstractSql sql = new TestSql();
        DbExecute execute = new MysqlExecute(sql);
        DbExecuteMonitor monitor = createMonitor(execute);
        monitor.loadWatcher(MemberWatcher.class);
        monitor.loadWatcher(Member2Watcher.class);
        monitor.loadWatcher(WatcherAll.class);
        monitor.attach(execute);

        Member member = new Member();
        Member member1 = new Member();
        Member2 m2 = new Member2();

        execute.insertby(Member2.class, i -> {
            i.id = 333;
        });
        execute.deleteby(Member2.class, i -> {
            i.id = 222;
        });
        execute.updateby(Member.class, (s, w) -> {
            s.name = "aaa";
            w.id = 111;
        });

        Task.delay(5000);
        System.out.println("测试完成");
        System.exit(0);
    }

    private static DbExecuteMonitor createMonitor(DbExecute execute) {
        DbExecuteMonitor monitor = new DbExecuteMonitor(new TypeResolver() {
            @Override
            public TypeResolver root() {
                return this;
            }

            @Override
            public TypeResolver sub(BeanDef... params) {
                return this;
            }

            @Override
            public void register(BeanDef def) {

            }

            @Override
            public <T> T resolve(Class<T> clazz) {
                if (clazz == MemberWatcher.class) {
                    return (T) new MemberWatcher(execute);
                }
                if (clazz == Member2Watcher.class) {
                    return (T) new Member2Watcher(execute);
                }
                if (clazz == WatcherAll.class) {
                    return (T) new WatcherAll();
                }
                return null;
            }

            @Override
            public List<Class<?>> getClassListForType(Class<?> clazz) {
                return null;
            }

            @Override
            public void close() {

            }
        });
        return monitor;
    }
}
